package com.woniuxy.dao;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUitl {
	// 将SqlSessionFactory对象涉及成单例的
	private static SqlSessionFactory sqlSessionFactory = null;

	public static SqlSession getSqlSession() {
		SqlSession sqlSession = null;
		try {
			if (sqlSessionFactory == null) {
				//加载主配置文件
				InputStream inputStream = Resources.getResourceAsStream("mybatisconfig.xml");
				//创建SqlSessionFactory的对象
				sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			}
			//创建SqlSession对象的,JDBCc操作数据库的核心对象
			sqlSession = sqlSessionFactory.openSession();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return sqlSession;

	}
}
